package com.cjst.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.rpc.Exporter;

//@Configuration
//@ConditionalOnClass(Exporter.class)
//@PropertySource(value = "classpath:/dubbo.properties")
public class DubboConfiguration {
	 private static Logger log=LoggerFactory.getLogger(DubboConfiguration.class);
	 @Value("${dubbo.application.name}")
	 private String applicationName;

	 @Value("${dubbo.registry.protocol}")
	 private String protocol;

	 @Value("${dubbo.registry.address}")
	 private String registryAddress;

	 @Value("${dubbo.protocol.name}")
	 private String protocolName;

	 @Value("${dubbo.protocol.port}")
	 private int protocolPort;

	 @Value("${dubbo.provider.timeout}")
	 private int timeout;

	 @Value("${dubbo.provider.retries}")
	 private int retries;

	 @Value("${dubbo.provider.delay}")
	 private int delay;
	 /**设置dubbo扫描包*/
	 @Bean
	 public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
		 log.info("=============================设置dubbo扫描包");
	     AnnotationBean annotationBean = new AnnotationBean();
	     annotationBean.setPackage(packageName);
	     return annotationBean;
	 }

	 /**dubbo上下文*/
	 @Bean
	 public ApplicationConfig applicationConfig() {
		 log.info("=============================dubbo上下文");
	     ApplicationConfig applicationConfig = new ApplicationConfig();
	     applicationConfig.setName(this.applicationName);
	     return applicationConfig;
	 }

	 /** 注册zookeeper*/
	 @Bean
	 public RegistryConfig registryConfig() {
		 log.info("=============================注册zookeeper");
	     RegistryConfig registry = new RegistryConfig();
	     registry.setProtocol(protocol);
	     registry.setAddress(registryAddress);
	     return registry;
	 }

	 /**协议 */
	 @Bean
	 public ProtocolConfig protocolConfig() {
		 log.info("=============================协议 ");
	     ProtocolConfig protocolConfig = new ProtocolConfig();
	     protocolConfig.setName(protocolName);
	     protocolConfig.setPort(protocolPort);
	     protocolConfig.setThreads(200);
	     //LogCore.RPC.info("protocolConfig:{}", protocolConfig.hashCode());
	     return protocolConfig;
	 }
	 /**服务消费者*/
	 @Bean(name="myConsumer")
	 public ConsumerConfig consumerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
		 log.info("=============================myConsumer");
		 ConsumerConfig consumerConfig = new ConsumerConfig();
		 consumerConfig.setTimeout(timeout);
		 consumerConfig.setRetries(retries);
		 consumerConfig.setCheck(false);
	     consumerConfig.setApplication(applicationConfig);
	     consumerConfig.setRegistry(registryConfig);
	     return consumerConfig;
	 }
	 /**服务提供者*/
	/* @Bean(name="myProvider")
	 public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
		 log.info("=============================myProvider");
	     ProviderConfig providerConfig = new ProviderConfig();
	     providerConfig.setTimeout(timeout);
	     providerConfig.setRetries(retries);
	     providerConfig.setDelay(delay);
	     providerConfig.setApplication(applicationConfig);
	     providerConfig.setRegistry(registryConfig);
	     providerConfig.setProtocol(protocolConfig);
	     return providerConfig;
	 }*/
}










